package com.rimi.controller;

import com.rimi.bean.*;
import com.rimi.commons.CommonResult;
import com.rimi.commons.ResultStatus;
import com.rimi.model.dto.PrescriptionListDto;
import com.rimi.model.vo.AddPreMedVo;
import com.rimi.model.vo.AddPrescriptionVo;
import com.rimi.model.vo.SetMedicalrecordsVo;
import com.rimi.model.vo.SetPrescriptionVo;
import com.rimi.service.PrescriptionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 处方
 */
@Controller
@CrossOrigin
@Api(description = "处方",tags = "处方")
public class PrescriptionController {

    @Autowired
    PrescriptionService prescriptionService;


    @PostMapping("prescription/addPrescription")
    @ResponseBody
    @ApiOperation("添加处方")
    public CommonResult addPrescription(@RequestBody AddPrescriptionVo addPrescriptionVo){

        Prescription prescription = new Prescription();
        BeanUtils.copyProperties(addPrescriptionVo, prescription);

        PreMed preMed = new PreMed();
        preMed.setMedicinalMoney(addPrescriptionVo.getMedicinalMoney());
        preMed.setMedicinalNum(addPrescriptionVo.getMedicinalNum());
        prescription.setAllPrice(preMed.getMedicinalMoney()*preMed.getMedicinalNum());

        CommonResult ResultPrescription = prescriptionService.addPrescription(prescription);
        //
        preMed.setPrescriptionId(prescription.getPrescriptionId());
        BeanUtils.copyProperties(addPrescriptionVo, preMed);

        if (ResultPrescription != null){
            CommonResult ResultPreMed = prescriptionService.addPreMed(preMed);
            return CommonResult.onSuccess(null);
        }
        //
        return CommonResult.onFailed(ResultStatus.ADD_FAILED);

    }


    @PostMapping("prescription/addPreMed")
    @ResponseBody
    @ApiOperation("添加药单")
    public CommonResult addPreMed(@RequestBody AddPreMedVo addPreMedVo){

        PreMed preMed = new PreMed();
        BeanUtils.copyProperties(addPreMedVo, preMed);
        return prescriptionService.addPreMed(preMed);
    }


    @GetMapping("user/prescription/showPrescription")
    @ResponseBody
    @ApiOperation("显示处方记录")
    public CommonResult<List<PrescriptionListDto>> showPrescription(){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<Prescription> allPrescriptionByUser = prescriptionService.findprescriptionByUserId(user.getUserId());
        ArrayList<PrescriptionListDto> dtos = new ArrayList<>();
        for (Prescription prescription : allPrescriptionByUser){
            PrescriptionListDto prescriptionListDto = new PrescriptionListDto();

            Appointment appointment = prescription.getAppointment();
            Doctor doctor = prescription.getDoctor();
            Pets pets = prescription.getPets();
            Medicinal medicinal = prescription.getMedicinal();
            PreMed preMed = prescription.getPreMed();
            prescriptionListDto.setAppointmentTime(appointment.getAppointmentTime());
            prescriptionListDto.setDoctorNickname(doctor.getDoctorNickname());
            prescriptionListDto.setPetNickname(pets.getPetNickname());
            prescriptionListDto.setMedicinalName(medicinal.getMedicinalName());
            prescriptionListDto.setMedicinalNum(preMed.getMedicinalNum());
            prescriptionListDto.setMedicinalMoney(medicinal.getMedicinalMoney());

            BeanUtils.copyProperties(prescription, prescriptionListDto);
            dtos.add(prescriptionListDto);
        }
        return CommonResult.onSuccess(dtos);
    }

    @DeleteMapping("user/prescription/deleteByUser")
    @ResponseBody
    @ApiOperation("删除处方记录（用户逻辑删除）")
    public CommonResult deleteByUser(String prescriptionId) {
        return prescriptionService.deletePrescriptionByUser(prescriptionId);
    }

    @PutMapping("prescription/setMedicalrecordsByUser")
    @ResponseBody
    @ApiOperation("更新就诊信息")
    public CommonResult setMedicalrecordsByUser(@RequestBody SetMedicalrecordsVo setMedicalrecordsVo){
        return prescriptionService.setMedicalrecordsByUser(setMedicalrecordsVo.getAppointmentTime(), setMedicalrecordsVo.getPetId());
    }

    @PutMapping("prescription/setPrescriptionByUser")
    @ResponseBody
    @ApiOperation("更新处方信息")
    public CommonResult setPrescriptionByUser(@RequestBody SetPrescriptionVo setPrescriptionVo){
        return prescriptionService.setPrescriptionByUser(setPrescriptionVo.getPrescriptionDate(), setPrescriptionVo.getPetsId());
    }
}
